﻿@using Newtouch.Tools;
@using Newtouch.Infrastructure;
@using Newtouch.HIS.Web.Core.Extensions;
@{
    ViewBag.Title = "出入库单查询";
    Layout = "~/Views/Shared/_Index.cshtml";

    var djlxItemStrs = Constants.CurrentYfbm.yfbmjb != "1" ?
        new[] {
(int) EnumDanJuLX.zhijiefayao,
(int) EnumDanJuLX.shenlingfayao,
(int) EnumDanJuLX.neibufayaotuihui,
(int) EnumDanJuLX.keshifayao
            }
            : new[] {
(int) EnumDanJuLX.yaopinruku,
(int) EnumDanJuLX.waibucuku,
(int) EnumDanJuLX.neibufayaotuihui,
(int) EnumDanJuLX.keshifayao
            };
}
<script src="~/Content/js/bootstrap-multiselect.js"></script>
<link href="~/Content/js/bootstrap-multiselect.css" rel="stylesheet" />
<style>
    #btn_search {
        height: 45px;
        width: 45px;
        background-color: #fff;
        color: #00a9ea;
        font-weight: bolder;
    }

    .showbotton {
        display: none;
        position: fixed;
        bottom: 5px;
        right: 0;
        z-index: 9999;
        background-color: #ccc;
        width: 50px;
        height: 44px;
        filter: alpha(Opacity=90);
        -moz-opacity: 0.9;
        opacity: 0.9;
        cursor: pointer;
        border-radius: 100px 0 0 100px;
    }

    #TabGrid {
        width: 100%;
        position: fixed;
        bottom: 50px;
        right: 0;
        z-index: 8888;
        background-color: #253443;
        filter: alpha(Opacity=92);
        -moz-opacity: 0.92;
        opacity: 0.92;
        box-shadow: 0px 2px 10px #909090;
    }

        #TabGrid > * {
            filter: alpha(Opacity=92);
            -moz-opacity: 0.92;
            opacity: 0.92;
        }

    input[name=fph] {
        padding: 2px;
    }
</style>
<form id="form1">
    <div class="panel panel-default" id="dv_search">
        <div class="panel-heading">查询条件</div>
        <div>
            <table class="form">
                <tr>
                    <th class="formTitle">审核状态：</th>
                    <td class="formValue">
                        @Html.DropDownList("ddlSHZT", EnumDjShzt.WaitingApprove.ToDescSelectList(), "全部", new { @class = "form-control" })
                    </td>
                    <th class="formTitle">日期：</th>
                    <td class="formValue">
                        <input id="txtStartDate" type="text" class="form-control input-wdatepicker " value="@DateTime.Now.ToString("yyyy-MM-01 00:00:00")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss' })" />
                    </td>
                    <th class="formTitle">至：</th>
                    <td class="formValue">
                        <input id="txtEndDate" type="text" class="form-control input-wdatepicker " value="@DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss' })" />
                    </td>
                    <th class="formTitle">发票号：</th>
                    <td class="formValue">
                        <input id="txtFPH" type="text" class="form-control " />
                    </td>
                    <td class="formTitle" rowspan="2">
                        <input type="button" id="btn_search" class="btn btn-md btn-primary" value="查询" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">单据号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="txtPDH" />
                    </td>
                    <th class="formTitle">单据类型：</th>
                    <td class="formValue">
                        @Html.DropDownList("ddlDJLX", EnumDanJuLX.yaopinruku.ToSomeDescSelectList(String.Join(",", djlxItemStrs)), "全部", new { @class = "form-control" })
                    </td>
                    <th class="formTitle">供应商：</th>
                    <td class="formValue">
                        <select id="selgys" name="selgys" class="form-control" multiple="multiple"></select>
                    </td>
                    <th class="formTitle">进价总额：</th>
                    <td class="formValue">
                        <input type="text" id="jjze" name="jjze" class="form-control newtouch_Readonly" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="gridPanel">
        <table id="gridDJMainRecords"></table>
        <div id="gridPager" rel="form1"></div>
    </div>
</form>
<div id="TabGrid">
    <ul class="nav nav-tabs navb-bg" role="tablist" id="myTab">
        <li role="presentation" class="active">
            <a href="#dv_djmx" id="linkSearch" role="tab" data-toggle="tab">单据明细</a>
        </li>
        <li style="float:right">
            <span class="hiding glyphicon glyphicon-chevron-right btn-lg"></span>
        </li>
    </ul>
    <div id="dv_djmx" style="">
        <table id="gridDJMXInfo"></table>
    </div>
</div>
<div class="showbotton">
    <span class="glyphicon glyphicon-chevron-left btn-lg"></span>
</div>
@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new int[] { 3, 6, 7, 8 },
    F3Text = "审核通过",
    F6Text = "批量审核通过",
    F7Text = "不通过",
    F8Text = "撤销审核"
})
<script>
    $(function () {
        //浏览器窗口调整大小时重新加载jqGrid的宽
        $(window).resize(function () {
            window.initLayout("MyTabGrid");
        });
        $('#ddlSHZT').val('0');
        //搜索框隐藏显示控制
        $(".hiding,.rows,#dv_search,.toolbar").click(function () {
            TabGridHide();
        });
        $(".showbotton").click(function () {
            $(".showbotton").animate({ width: 'hide' }, "fast", function () {
                $("#TabGrid").animate({ left: '0px' }, "slow");
            });
        });
        TabGridHide();
        gridDJMainRecords();
        BindGys();
    });

    //绑定供应商
    function BindGys() {
        $.najax({
            type: "Get",
            url: "/Medicine/MedicineSupplierList?keyword=",
            dataType: "json",
            success: function (ajaxresp) {
                if (ajaxresp !== null) {
                    $("#selgys").html("");
                    for (var i = 0; i < ajaxresp.length; i++) {
                        $("#selgys").append("<option value='" + ajaxresp[i].gysCode + "'>" + ajaxresp[i].gysmc + "</option>");
                    }
                    $('#selgys').multiselect({
                        buttonWidth: '100%',
                        includeSelectAllOption: true,
                        enableClickableOptGroups: true,
                        enableCollapsibleOptGroups: true,
                        selectAllText: "全选",
                        nonSelectedText: "未选中",
                        allSelectedText: "全选"
                    });
                }
            }
        });
    }

    //隐藏搜索框并显示箭头
    function TabGridHide() {
        var tabGridwith = $("#TabGrid").width() + 200;
        if ($(".showbotton").is(":hidden")) {
            $("#TabGrid").animate({ left: tabGridwith }, "slow", function () {
                $(".showbotton").animate({ width: 'show' }, "fast");
            });
        }
    }

    //单据主记录
    function gridDJMainRecords() {
        var $gridDjMainRecords = $("#gridDJMainRecords");
        $gridDjMainRecords.dataGrid({
            height: $(window).height() - 230,
            postData: getSearchPostData(),
            url: "/PharmacyDrugStorage/SelectReceiptMainInfoV2?from=approval",
            colModel: [
                {
                    label: '审核状态', name: 'shzt', width: 55, align: 'left', formatter: function (val) {
                        switch (val) {
                        case '@((int)EnumDjShzt.WaitingApprove)':
                            return "@EnumDjShzt.WaitingApprove.GetDescription()";
                        case '@((int)EnumDjShzt.Approved)':
                            return "@EnumDjShzt.Approved.GetDescription()";
                        case '@((int)EnumDjShzt.Cancelled)':
                            return "@EnumDjShzt.Cancelled.GetDescription()";
                        case '@((int)EnumDjShzt.Rejected)':
                            return "@EnumDjShzt.Rejected.GetDescription()";
                        default:
                            return "";
                        }
                    }
                },
                {
                    label: '单据类型', name: 'djlx', width: 80, align: 'left', formatter: function (val) {
                        switch (val) {
                        case @((int)EnumDanJuLX.zhijiefayao):
                            return "@EnumDanJuLX.zhijiefayao.GetDescription()";
                        case @((int)EnumDanJuLX.keshifayao):
                            return "@EnumDanJuLX.keshifayao.GetDescription()";
                        case @((int)EnumDanJuLX.neibufayaotuihui):
                            return "@EnumDanJuLX.neibufayaotuihui.GetDescription()";
                        case @((int)EnumDanJuLX.shenlingfayao):
                            return "@EnumDanJuLX.shenlingfayao.GetDescription()";
                        case @((int)EnumDanJuLX.waibucuku):
                            return "@EnumDanJuLX.waibucuku.GetDescription()";
                        case @((int)EnumDanJuLX.yaopinruku):
                            return "@EnumDanJuLX.yaopinruku.GetDescription()";
                        case @((int)EnumDanJuLX.shenqingdiaobo):
                            return "@EnumDanJuLX.shenqingdiaobo.GetDescription()";
                        default:
                            return "";
                        }
                    }
                },
                { label: '单据号', name: 'pdh', width: 130, align: 'center' },
                { label: '出库库房', name: 'ckbmmc', width: 100, align: 'left' },
                { label: '供应商', name: 'gysmc', width: 100, align: 'left' },
                { label: '入库库房', name: 'rkbmmc', width: 100, align: 'left' },
                { label: '出入库方式', name: 'crkfsmc', width: 70, align: 'left' },
                { label: '批发总额', name: 'pjze', width: 80, align: 'right', formatter: function (val) { return val.toFixed(2); }, hidden: true },
                { label: '零售总额', name: 'ljze', width: 80, align: 'right', formatter: function (val) { return val.toFixed(2); }, hidden: true },
                { label: '进价总额', name: 'zje', width: 80, align: 'right', formatter: function (val) { return val.toFixed(2); } },
                { label: '进销差价', name: 'jxcj', width: 70, align: 'right', formatter: function (val) { return val.toFixed(2); }, hidden: true},
                { label: '提交时间', name: 'czsj', width: 100, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:s', newformat: 'Y-m-d H:s' } },
                { label: '入库时间', name: 'Rksj', width: 100, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:s', newformat: 'Y-m-d H:s' } },
                { label: '出库时间', name: 'Cksj', width: 100, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:s', newformat: 'Y-m-d H:s' } },
                { label: 'crkId', name: 'crkId', hidden: true },
                { label: 'djlx', name: 'djlx', hidden: true, align: 'left' },
                { label: 'jjzje', name: 'jjzje', hidden: true, align: 'left' }
            ],
            unwritten: false,
            multiselect: false,
            pager: "#gridPager",
            sortname: 'czsj desc',
            viewrecords: true,
            gridComplete: function() {
                ComputeJJze();
            },
            ondblClickRow: function (rowid, iRow, iCol, e) {
                btn_selectrow(rowid);
            }
        });

        $("#btn_search").click(function () {
            $('#gridDJMXInfo').jqGrid("clearGridData");
            $gridDjMainRecords.jqGrid('setGridParam', {
                postData: getSearchPostData(),
                url: "/PharmacyDrugStorage/SelectReceiptMainInfoV2?from=approval"
            }).trigger('reloadGrid');
        });
    }

    //组装查询条件
    function getSearchPostData() {
        var shzt = $("#ddlSHZT").val();
        var qsrj = $("#txtStartDate").val();
        var jsrj = $("#txtEndDate").val();
        var pdh = $("#txtPDH").val();
        var djlx = $("#ddlDJLX").val();
        var fph = $("#txtFPH").val();
        var gys = $("#selgys").val();
        return { qsrj: qsrj, jsrj: jsrj, pdh: pdh, djlx: djlx, shzt: shzt, gys: gys, fph: fph};
    };

    //单击事件
    function btn_selectrow(rowId) {
        $('#gridDJMXInfo').jqGrid("clearGridData");//每次单击，先清空明细 再重新加载所有选中行明细
        $.najax({
            type: "Get",
            url: "/BillManage/OutOrInStorageBill/QueryOutOrInStorageBillDetail",
            data: { crkId: $("#gridDJMainRecords").jqGrid('getRowData', rowId).crkId, djlx: $("#gridDJMainRecords").jqGrid('getRowData', rowId).djlx },
            dataType: "json",
            success: function (ajaxresp) {
                if (ajaxresp !== null) {
                    $('#gridDJMXInfo').newtouchLocalDataGrid(null, ajaxresp);
                }
                $(".showbotton").click().trigger("click");
            }
        });
    }

    //单据明细
    $('#gridDJMXInfo').newtouchLocalDataGrid({
        height: $(window).height() - 240,
        unwritten: false,
        sortable: false,
        colModel: [
            { label: '出入库明细序号', name: 'crkmxId', hidden: true, width: 100, align: 'left' },
            {
                label: '发票号', name: 'fph', width: 90, align: 'center',
                formatter: function (cellvalue, options) { return cellvalue === "" || cellvalue === null ? "<input type=\"text\" name=\"fph\" rowid=" + options.rowId + " role=\"textbox\" class=\"editable inline-edit-cell form-control\" style=\"width: 90%; height: 99%;\" onblur=\"UpdateFph(this)\">" : cellvalue; }
            },
            { label: '药品类别', name: 'yplbmc', width: 60, align: 'left' },
            { label: '药品名称', name: 'ypmc', width: 110, align: 'left' },
            { label: '数量', name: 'slanddw', width: 60, align: 'right' },
            { label: '规格', name: 'gg', width: 80, align: 'left' },
            { label: '批号', name: 'ph', width: 90, align: 'center' },
            { label: '批次', name: 'pc', width: 100, align: 'center' },
            { label: '有效期', name: 'yxq', width: 70, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
            { label: '进价单价', name: 'jjdwdj', width: 90, align: 'right' },
            { label: '批发总额', name: 'pjze', width: 70, align: 'right', formatter: function (val) { return val.toFixed(2); } },
            { label: '零售总额', name: 'ljze', width: 70, align: 'right', formatter: function (val) { return val.toFixed(2); } },
            { label: '进价总额', name: 'zje', width: 70, align: 'right', formatter: function (val) { return val.toFixed(2); } },
            { label: '进销差价', name: 'jxcj', width: 60, align: 'right', formatter: function (val) { return val.toFixed(2); } },
            { label: '生产厂家', name: 'sccj', width: 150, align: 'left' },
            { label: '退货原因', name: 'thyy', width: 80, align: 'left' },
            { label: '进价单价', name: 'jj', hidden: true }
        ]
    });

    //计算进价总额
    function ComputeJJze() {
        var t = 0;
        var count = $("#gridDJMainRecords").getGridParam('records');
        if (count > 0) {
            var rowIds = $("#gridDJMainRecords").getDataIDs();
            t = $("#gridDJMainRecords").getRowData(rowIds[0]).jjzje;
            $("#jjze").val(t);
        } else {
            $("#jjze").val('');
        }
    }

    //审核
    function newtouch_event_f3() {
        submitReceiptApproval('@((int)EnumDjShzt.Approved)');
    }

    //批量审核
    function newtouch_event_f6() {
        var ids = $("#gridDJMainRecords").jqGrid('getDataIDs'); //获取所有行Id，遍历使编辑框处于保存状态
        if (ids == null || ids.length === 0) {
            $.modalMsg('请查询要审核的单据', 'warning', 2000);
            return false;
        }
        var shdj = [];
        for (var i = 0; i < ids.length; i++) {
            var selRowData = $("#gridDJMainRecords").jqGrid('getRowData', ids[i]);
            if (selRowData.crkId && selRowData.djlx) {
                var item = {
                    crkId: selRowData.crkId,
                    djlx: selRowData.djlx,
                    shzt: '@((int)EnumDjShzt.Approved)'
                };
                shdj.push(item);
            }
        }
        if (shdj.length === 0) {
            $.modalMsg('未找到满足审核要求的单据', 'warning', 2000);
            return false;
        }

        $.modalConfirm("确定批量审核吗？批量审核只针对当前显示页单据！",
            function(flag) {
                if (!flag) return false;
                $.najax({
                    url: "/OutOrInStorageBill/SubmitReceiptApprovalBatch",
                    data: { param: shdj },
                    type: 'POST',
                    dataType: 'json',
                    success: function (ajaxresp) {
                        if (ajaxresp.state === 'success') {
                            $.modalMsg("操作成功", 'success', 1500);
                        }
                        else {
                            $.modalMsg("操作失败", 'error', 1500);
                            $.newtouchAlert(ajaxresp);
                        }
                        return;
                    },
                    complete: function () {
                        $("#btn_search").trigger('click');
                    }
                });
            });
    }

    //未通过
    function newtouch_event_f7() {
        submitReceiptApproval('@((int)EnumDjShzt.Rejected)');
    }

    //作废
    function newtouch_event_f8() {
        submitReceiptApproval('@((int)EnumDjShzt.Cancelled)');
    }

    //更新单据审核状态，提交至服务器
    function submitReceiptApproval(dstnShzt) {
        var selRowData = $("#gridDJMainRecords").jqGridRowValue();
        if (!selRowData || !selRowData.crkId || !selRowData.djlx) {
            $.modalAlert('请选择一条单据', 'error');
            return;
        }
        if (selRowData.crkId && selRowData.djlx) {
            $.najax({
                url: "/OutOrInStorageBill/SubmitReceiptApproval",
                data: { crkId: selRowData.crkId, djlx: selRowData.djlx, shzt: dstnShzt, pdh: selRowData.pdh },
                type: 'POST',
                dataType: 'json',
                success: function (ajaxresp) {
                    if (ajaxresp.state === 'success') {
                        $.modalMsg("操作成功", 'success', 1500);
                    }
                    else {
                        $.modalMsg("操作失败", 'error', 1500);
                        $.newtouchAlert(ajaxresp);
                    }
                    return;
                },
                complete: function () {
                    $("#btn_search").trigger('click');
                }
            });
        }
    }


    //补发票号
    function UpdateFph(obj) {
        var fph = $(obj).val();
        var crkmxId = $("#gridDJMXInfo").getRowData($(obj).attr("rowid")).crkmxId;
        if (fph !== "") {
            $.modalConfirm("您确定要将发票号改为" + fph + "吗？",
                function(flag) {
                    if (flag) {
                        $.najax({
                            url: "/PharmacyDrugStorage/UpdateFph",
                            type: "POST",
                            data: { crkmxId: crkmxId, fph: fph },
                            dataType: "json",
                            success: function(data) {
                                if (data.state === 'success') {
                                    $.modalMsg("发票号修改成功", "success", 1000);
                                } else {
                                    $.newtouchAlert(data);
                                }
                            }
                        });
                    }
                });

        }
    }
</script>